<?php

namespace App\Http\Controllers;

use App\Models\Schedule;
use Illuminate\Support\Facades\Auth;
use App\Repositories\ScheduleRepository;
use App\Http\Requests\Logic\Schedule\QueryResource;
use App\Http\Requests\Logic\Schedule\StoreResource;
use App\Http\Requests\Logic\Schedule\UpdateResource;
use App\Http\Requests\Logic\Schedule\DestroResource;
use App\Http\Requests\Logic\Schedule\QueryCollection;

/**
 * 排班逻辑控制器
 *
 * Date: 22/04/2018
 * @author George
 * @package App\Http\Controllers
 */
class ScheduleController extends Controller
{
    /**
     * 获取排班数据集合
     *
     * Date: 22/04/2018
     * @author George
     * @param QueryCollection $request
     * @return \Illuminate\Http\JsonResponse
     */
	public function index(QueryCollection $request)
	{
	    $user = Auth::user();
	    $user_id = $request->get('user_id');
	    $project_id = $request->get('project_id', $user->project->id);
	    $start = $request->get('start');
	    $end = $request->get('end');
	    $paginate = $request->get('paginate', 15);
	    $company_id = $user->company_id;

	    $query = Schedule::query()->with('project:id,name', 'user:id,name')->where('company_id', $company_id);

	    if ($user_id) {
	        $query->where('user_id', $user_id);
        }

        if ($project_id) {
            $query->where('project_id', $project_id);
        }

        if ($start && $end) {
	        $query->whereBetween('start',[$start, $end])->whereBetween('end', [$start, $end]);
        }

        return success($query->paginate($paginate));
	}

    /**
     * 根据ID获取排班详情
     *
     * Date: 22/04/2018
     * @author George
     * @param QueryResource $request
     * @param Schedule $schedule
     * @return \Illuminate\Http\JsonResponse
     */
	public function show(QueryResource $request, Schedule $schedule)
	{
	    return success($schedule);
	}

	/**
	 * 创建排班记录
	 *
	 * Date: 25/04/2018
	 * @author George
	 * @param StoreResource $request
	 * @param ScheduleRepository $scheduleRepository
	 * @return \Illuminate\Http\JsonResponse
	 * @throws \App\Exceptions\GeneralException
	 */
	public function store(StoreResource $request, ScheduleRepository $scheduleRepository)
	{
		$attributes = $this->extractInputFromRules($request, $request->rules());
		if ($scheduleRepository->verification($attributes['project_id'], $attributes['demand_id'], $attributes['users_id']) > 0) {
			return failed('排班记录已存在');
		}

		$schedule = $scheduleRepository->store($attributes);

		return stored($schedule);
	}

    /**
     * 更新排班信息
     *
     * Date: 22/04/2018
     * @author George
     * @param UpdateResource $request
     * @param Schedule $schedule
     * @return \Illuminate\Http\JsonResponse
     */
	public function update(UpdateResource $request, Schedule $schedule)
	{
	    $attributes = $this->extractInputFromRules($request, $request->rules());
	    $schedule->update($attributes);
	    return updated($attributes);
	}

    /**
     * 删除排班信息
     *
     * Date: 22/04/2018
     * @author George
     * @param DestroResource $request
     * @param Schedule $schedule
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
	public function destroy(DestroResource $request, Schedule $schedule)
	{
	    $schedule->delete();
	    return deleted();
	}

    /**
     * 验证给定日期是否存在
     *
     * Date: 21/04/2018
     * @author George
     * @param $user_id
     * @param string $start
     * @param string $end
     * @return bool
     */
	protected function leisure($user_id, string $start, string $end)
    {
        $count =  Schedule::query()->select(['id'])
            ->where('user_id', $user_id)
            ->whereBetween('start', [$start, $end])
            ->whereBetween('end', [$start, $end])
            ->count();
        return $count > 0 ? false : true;
    }
}
